package com.baosight.risk.base.controller;


import com.baosight.iplat4j.core.web.threadlocal.UserSession;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.ServletUtils;
import com.baosight.risk.common.utils.ShiroUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.web.page.PageDomain;
import com.baosight.risk.web.page.TableSupport;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author duanyongqiang
 * @date 2019.3.26
 * <p>
 * web层通用数据处理
 */
public class BaseController {

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }


    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageIndex();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageIndex) && StringUtils.isNotNull(pageSize)) {
            String orderBy = pageDomain.getSortField();
            PageHelper.startPage(pageIndex, pageSize, orderBy);
        }
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage(String sortField, String sortOrder) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageIndex = pageDomain.getPageIndex();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageIndex) && StringUtils.isNotNull(pageSize)) {
            String orderBy = StringUtils.toUnderScoreCase(sortField) + " " + sortOrder;
            PageHelper.startPage(pageIndex, pageSize, orderBy);
        }
    }


    /**
     * 响应请求分页数据
     */
    protected AjaxResult getResultPage(List<?> list) {
        AjaxResult result = new AjaxResult();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", new PageInfo(list).getTotal());
        map.put("data", list);
        result.put("msg", "查询结果");
        result.put("code", 200);
        result.put("data", map);
        return result;
    }

    /**
     * 获取当前登陆用户名称
     *
     * @return
     */
    public String getUserName() {
        return ShiroUtils.getLoginName();
    }

    /**
     * 获取当前登陆用户名称
     *
     * @return
     */
    public XtzyUser getUser() {
        return ShiroUtils.getUser();
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success() {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error() {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message) {
        return AjaxResult.success(message);
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message, Object data) {
        return AjaxResult.success(message, data);
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(Object data) {
        return AjaxResult.success("成功", data);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message) {
        return AjaxResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public AjaxResult error(int code, String message) {
        return AjaxResult.error(code, message);
    }


}
